//
// Created by root on 2/18/18.
//

#include <stdlib.h>
#include <stdio.h>
#include "stack.h"
#include "../base/cmemory.h"


StackNode *alloc_stack_node() {
    return (StackNode *) sys_malloc(sizeof(StackNode));
}

Stack *init_stack() {
    StackNode *node =  alloc_stack_node();
    node->element = NULL;
    node->next = NULL;
    return node;
}

int is_stack_empty(Stack *stack) {
    return (stack->next == NULL);
}

void push(Stack **stack, void *element) {
    StackNode *stackNode = alloc_stack_node();
    stackNode->element = element;
    stackNode->next = *stack;
    *stack = stackNode;
}

void *pop(Stack **stack) {
    if (is_stack_empty(*stack)) {
        fprintf(stderr, "Error: Stack is empty.\n");
        return NULL;
    }
    StackNode *stackNode = *stack;
    *stack = stackNode->next;
    return stackNode->element;
}
